package com.huikeportal.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.dao.FootfallGlobalMapper;
import com.huikeportal.model.ReportGraphData;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.MemcachedService;
import com.huikeportal.service.SquareWeekService;

/**
 * 
 * @author wenyp
 *	星期对比
 */
@Service("SquareWeekService")
public class SquareWeekServiceImpl implements SquareWeekService{
	
	@Autowired
	private FootfallGlobalMapper footfallGlobalMapper;
	
	@Autowired
	private FootfallGlobalService footfallGlobalService;
	
	@Autowired
	private MemcachedService memcachedService;
	
	
	/**
	 * 查询图表数据
	 * @param map
	 * @return List<ReportGraphData>
	 * @throws ParseException
	 */
	@SuppressWarnings("unchecked")
	public List<HashMap<String, Object>> getGraphData(HashMap<String, Object> map)  throws Exception{
		List<ReportGraphData> resultList=new ArrayList<ReportGraphData>();
		//依据登陆用户信息查询用户场所
//		SiteAllInfo siteAllInfo=selectSiteByUserId((UserInfo)map.get("userInfo"));
		
		String timeType="";
		if(map.get("timeType")!=null && !map.get("timeType").toString().equals("")){
			timeType=map.get("timeType").toString();
		}
		String timeType_table="";
		if(map.get("timeType_table")!=null && !map.get("timeType_table").toString().equals("")){
			timeType_table=map.get("timeType_table").toString();
		}
		String startDate="";
		String endDate="";
		String from="";
		if(map.get("startDate")!=null && !map.get("startDate").toString().equals("")){
			from=map.get("startDate").toString();
		}
		String to="";
		if(map.get("endDate")!=null && !map.get("endDate").toString().equals("")){
			to=map.get("endDate").toString();
		}
		String type="";
		if(map.get("type")!=null && !map.get("type").toString().equals("")){
			type=map.get("type").toString();
		}
		String siteId="";
		if(map.get("siteId")!=null && !map.get("siteId").toString().equals("")){
			siteId=map.get("siteId").toString();
		}
		
		List<HashMap<String, Object>> resultMapOfList=new ArrayList<HashMap<String,Object>>();
		
		List<String> list=new ArrayList<String>();
		if("1".equals(timeType)){
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); 
			String yearWeek=String.valueOf(cal.get(Calendar.YEAR))+cal.get(Calendar.WEEK_OF_YEAR);
			list.add(yearWeek);
			
		}else if("2".equals(timeType)){
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -7);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); 
			String yearWeek=String.valueOf(cal.get(Calendar.YEAR))+cal.get(Calendar.WEEK_OF_YEAR);
			list.add(yearWeek);
		}else if("3".equals(timeType)){
			Calendar cal1 = Calendar.getInstance();
			cal1.add(Calendar.DATE, -7);
			cal1.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); 
			String yearWeek1=String.valueOf(cal1.get(Calendar.YEAR))+cal1.get(Calendar.WEEK_OF_YEAR);
			list.add(yearWeek1);
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -14);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); 
			String yearWeek=String.valueOf(cal.get(Calendar.YEAR))+cal.get(Calendar.WEEK_OF_YEAR);
			list.add(yearWeek);
		}else {
//			计算周数
			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");

			Calendar clFrom = Calendar.getInstance(); 
			clFrom.setTime(simpleDateFormat.parse(from));
			clFrom.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); 
			Calendar clTo = Calendar.getInstance(); 
			clTo.setTime(simpleDateFormat.parse(to));
			clTo.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); 
			
			String beginYear=String.valueOf(clFrom.get(Calendar.YEAR));
			String beginWeek=String.valueOf(clFrom.get(Calendar.WEEK_OF_YEAR));
			String endYear= String.valueOf(clTo.get(Calendar.YEAR));
			String endWeek=String.valueOf(clTo.get(Calendar.WEEK_OF_YEAR));
			list=footfallGlobalService.getWeeksBetweenTwoWeek(beginYear,beginWeek,endYear,endWeek);
		}
		
		for(String str:list){
			String endDateOfWeek =getSunday(str);
			String startDateOfWeek =getMonday(str);
		
		
		HashMap<String,Object> typeMap =footfallGlobalService.getCalret(Integer.parseInt(type));
		//根据日周月年的不同需要的参数map
		HashMap<String, Object> pramMap1=getparam("1",startDateOfWeek,endDateOfWeek);
		pramMap1.put("siteid",siteId);//放入场所id
		pramMap1.put("calret", typeMap.get("calret"));
		pramMap1.put("ziduan", typeMap.get("ziduan"));
		

		resultList =footfallGlobalMapper.selectGraphDataDay(pramMap1);
		//和应该有的日期对比，如果该日期没有数据设置为空，添加到list
		List<String> listDate=(List<String>)pramMap1.get("Betweens");
		if(resultList.size()!=listDate.size()){
			for (int da=0;da<listDate.size();da++){
				Boolean flg=false;
				for(int rep=0;rep< resultList.size();rep++){
					if(listDate.get(da).equals(resultList.get(rep).getDatatime())){
						flg=true;
					}
				}
				if(flg){
					continue;
				}else{
				ReportGraphData reportGraphData=new ReportGraphData();
				reportGraphData.setDatatime(listDate.get(da));
				reportGraphData.init();
				resultList.add(da, reportGraphData);
				}
			}
		}
		HashMap<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("yearWeek", str);
		resultMap.put("weeknum", pramMap1.get("week"));
		resultMap.put("resultList",resultList);
		
		resultMapOfList.add(resultMap);
		}
		
		return resultMapOfList;
	}

	/**
	  * 得到某周周日n为推迟的周数，1本周周末，0上周周末 -1上上周周末，依次类推
	  * @param int n
	  * @return yyyy-MM-dd
	  */
	
	public static String getSundayOfWeek(int n){
		//n为推迟的周数，1本周周末，0上周周末 -1上上周周末，依次类推
		Calendar cal = Calendar.getInstance();
		String monday;
		cal.add(Calendar.DATE, n*7);
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	
	/**
	  * 得到某周周日n为推迟的周数，1本周周末，0上周周末 -1上上周周末，依次类推
	  * @param String yyyy-ww
	  * @return yyyy-MM-dd
	  */
	
	public static String getSunday(String weekOfYear){
		String year=weekOfYear.substring(0, 4);
		String week =weekOfYear.substring(4);
		//n为推迟的周数，1本周周末，0上周周末 -1上上周周末，依次类推
		Calendar cal = Calendar.getInstance();
		String monday;
		cal.set(Calendar.YEAR,Integer.parseInt(year));
		cal.set(Calendar.WEEK_OF_YEAR,Integer.parseInt(week));
		cal.add(Calendar.DATE, 7);
		cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	public static String getMonday(String weekOfYear){
		String year=weekOfYear.substring(0, 4);
		String week =weekOfYear.substring(4);
		//n为推迟的周数，1本周周末，0上周周末 -1上上周周末，依次类推
		Calendar cal = Calendar.getInstance();
		String monday;
		cal.set(Calendar.YEAR,Integer.parseInt(year));
		cal.set(Calendar.WEEK_OF_YEAR,Integer.parseInt(week));
		cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	
	/**
	  * 得到某周周一,n为推迟的周数，0本周周一，-1上周周一，-2是上上周周一，依次类推
	  * @param int n
	  * @return yyyy-MM-dd
	  */
	
	public static String getMondayOfWeek(int n){
		//n为推迟的周数，1本周，0是上周，-1上上周，依次类推
		Calendar cal = Calendar.getInstance();
		String monday;
		cal.add(Calendar.DATE, n*7);
		//想周几，这里就传几Calendar.MONDAY（TUESDAY...）
		cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
		return monday;
	}
	
	/**
	  * 得到数据起始结束时间、及时间点list
	  * @return HashMap<String, Object>
	  */
	public HashMap<String, Object> getparam(String timeType,String from,String to) throws ParseException{
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		HashMap<String, Object> resultMap = new HashMap<String, Object>();
		Calendar cal=Calendar.getInstance();
		cal.setTime(sf.parse(from));

		resultMap.put("week", cal.get(Calendar.WEEK_OF_YEAR));
		//根据timeType计算查询几天的数据
		if(timeType.equals("1")){//本周周一到周末
			Date dateFrom= sf.parse(from);
			Date dateTo= sf.parse(to);
			resultMap.put("startDate",sf.format(dateFrom));
			resultMap.put("endDate", sf.format(dateTo));
		}else if(timeType.equals("2")){//上周周一到周末
			Date date=new Date();
			String endDate=sf.format(date);
			date.setDate(date.getDate()-7);
			String startDate=sf.format(date);
			resultMap.put("startDate", startDate);
			resultMap.put("endDate", endDate);
		}else if(timeType.equals("3")){//上上周周一到周末
			Date date=new Date();
			String endDate=sf.format(date);
			date.setDate(date.getDate()-14);
			String startDate=sf.format(date);
			resultMap.put("startDate", startDate);
			resultMap.put("endDate", endDate);
			
		}
		//根据开始时间结束时间算出时间段
		List<String> listDate=footfallGlobalService.getDatesBetweenTwoDate(resultMap.get("startDate").toString(),resultMap.get("endDate").toString());
		resultMap.put("Betweens", listDate);
		return resultMap;
	}
	
}
